define(function (require, exports, module) {
    require("apps/pbom/page.css");
    require("apps/grid");

    exports.init = function (ctx, uuid, config) {

        var pageListComponent = new $.PageTreeListComponent(uuid, {
            pagination:false,
            view: {
                grid: "grid",
                myToolbar: "news_tb",
                colbar: "user_list_btn"
            },
            configs: {
                gridUrl: ctx + "/app/api/ebom/list?id=" + config.id,
                idField: "id",
                treeField: "eName",
                gridColumns: [
                    {
                        field: 'id',
                        checkbox: true
                    },
                    {
                        field: 'temp1',
                        hidden: true
                    },
                    {
                        field: 'eName',
                        title: '名称',
                        width: 100,
                        align: 'left',
                        formatter: function (value, row, index) {
                            return value + (row.status == 'RECYCLE' ? "（已回收）" : "");
                        }
                    },
                    {
                        field: 'code',
                        title: '编码',
                        width: 100,
                        align: 'left'
                    },
                    {
                        field: 'pBomDes',
                        title: '设计结构',
                        width: 100,
                        align: 'left'
                    },
                    {
                        field: "cover",
                        title: "图片",
                        hidden: true,
                        formatter: function (value, row, index) {
                            return "<img src=\"" + ctx + "/print/" + value + "\" style=\"width:48px;height:32px;\"/>"
                        }
                    },
                    {
                        field: 'bomType',
                        title: '是否装配',
                        width: 100,
                        align: 'left',
                        formatter: function (value, row, index) {
                            if (value == "E_BOM_TYPE_BUILD") {
                                return "已装配";
                            } else {
                                return "未装配";
                            }
                        }
                    },
                    {
                        field: 'remark',
                        title: '备注',
                        hidden: true,
                        width: 50,
                        align: 'left'
                    },
                    {
                        field: 'description',
                        title: '功能',
                        width: 50,
                        align: 'left',
                        hidden: true
                    },
                    {
                        field: 'createTime',
                        title: '创建时间',
                        width: 100,
                        align: 'left'
                    }
                ],
                onExpand: function (row) {
                    if (row.children == undefined) {
                        pageListComponent.grid.treegrid("loadChildren", {
                            url: ctx + "/app/api/ebom/children",
                            row: row
                        });
                    }
                }
            },
            events: {
                renderColBarBefore: function (key, row) {
                    return true;
                },
                onClickColBtn: function (context, btn, row) {
                    switch (btn.key) {
                        case "view":
                            whenView(context, row);
                            break;
                        default :
                            break;
                    }
                }
            }
        });
        var globalWindow = new $.PageWindow();

        function whenViewDetail(context, row) {
            //获取组成连接
            var pa = context.grid.treegrid("getParent", row.id);
            var parentId = pa.refId ? pa.refId : pa.id,
                curId = row.refId ? row.refId : row.id;
            globalWindow.create({
                width: 700,
                href: ctx + "/app/api/ebom/pBomDetailList?parentId=" + parentId + "&curId=" + curId,
                title: pa.eName + "(" + pa.code + ")" + "的组成结构"
            });
            globalWindow.open();
        }

        function whenView(context, row) {
            if(!row.bomGroup){
                context.detail("工艺装配结构详情查看", row);
            }else{
                whenViewDetail(context, row);
            }
        }
    }
});